<template>
	<view class="goods-detail">
		<swiper :autoplay="true" :interval="3000" :duration="1000" @change="swiperChange" style="border-radius: 20rpx;">
			<swiper-item v-for="(item,index) in bannerList" :key="index">
				<view class="swiper-item" @click="goBanner(item)" :data-idx="index+1">
					<image :src="item" style="width: 100%; height: 100%;border-radius: 20rpx;"></image>
				</view>
			</swiper-item>
		</swiper>
		<view class="goods-info">
			<view class="goods-title">
				{{goodsData.description}}
			</view>
			<view class="goods-html" v-html="goodsData.html">

			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onMounted, ref, reactive, watch, nextTick, onActivated } from "vue";
	import { onLaunch, onShow, onLoad, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";
	import { getProduct, getPhone, getShare, setUser, doAuthSilent, doWxAuth } from "@/utils/api";
	const bannerList = ref([]);
	const goodsData = ref({});
	const swiperCurrent = ref(0)
	const loginData = reactive({
		codeWx: '',
		encryptedData: '',
		iv: ''
	})
	function getData() {
		getProduct({}).then((res : any) => {

		})
	}
	function swiperChange(e) {
		swiperCurrent.value = e.detail.current
	}
	function login() {
		uni.login({
			provider: 'weixin',
			success: (res) => {
				if (res.code) {
					//微信登录成功 已拿到code
					loginData.codeWx = res.code;
					uni.getUserInfo({
						provider: 'weixin',
						success: function (infoRes) {
							loginData.encryptedData = infoRes.encryptedData;
							loginData.iv = infoRes.iv;
							doWxAuthFn();
						}
					})
				} else {
					console.log("登录失败！" + res.errMsg);
				}
			},
		});
	}
	function doWxAuthFn() {
		const params = {
			jsCode: loginData.codeWx,
			encryptedData: loginData.encryptedData,
			siv: loginData.iv,
		}
		doWxAuth(params).then(res => {
			if (res.code == 200) {
				uni.setStorageSync('access_token', res.data.token)
				uni.setStorageSync('userId', res.data.id)
				uni.setStorageSync('userData', {
					mobile: res.data.mobile,
					companyType: res.data.companyType,
					userName: res.data.userName
				})
				uni.showToast({
					title: '登录成功！',
					icon: 'success',
					duration: 2000
				})
			}
		})
	}
	onLoad(() => {
		getData();
		login()
	})
</script>

<style lang="scss" scoped>
	.goods-detail {
		.goods-info {
			width: 100vw;
			box-sizing: border-box;
			padding: 0 20rpx 20rpx 20rpx;

			.goods-video {
				width: 100%;

				video {
					width: 100%;
					border-radius: 10px;
				}
			}

			.goods-html {
				word-break: break-all;

			}
		}
	}
</style>